package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.OrderPageRequest;
import com.example.springboot.entity.Order;
import com.example.springboot.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin  //解决跨域错误
@RestController
@RequestMapping("/order")
//是rest风格的ordercontroller
public class OrderController {

    @Autowired
    IOrderService orderService;

    //    OrderPageRequest传入用户名和电话信息
    @GetMapping("/page")
    public Result page(OrderPageRequest orderPageRequest){
        //        返回数据 Result.success传入一个对象
        return Result.success(orderService.page(orderPageRequest));
    }

//    post put get delete对应增改查删四类操作
    //    新增订单
    @PostMapping("/save")
    public Result save(@RequestBody Order order){
        orderService.save(order);
        return Result.success();
    }

//    更新订单信息
    @PutMapping("/update")
    public Result update(@RequestBody Order order){
        orderService.update(order);
        return Result.success();
    }

    //    获取订单列表
    @GetMapping("/list")
    public Result list(){
        //        返回数据
        List<Order> orders=orderService.list();
//        System.out.println("orders内容是"+orders);
        return Result.success(orders);
    }

//    根据ID更新订单信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Order order =orderService.getById(id);
        return Result.success(order);
    }

//    根据用户名来操作
    @GetMapping("/{userName}")
    public Result getByUserName(@PathVariable String userName){
        Order order =orderService.getByUserName(userName);
        return Result.success(order);
    }

    //删除操作:
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        orderService.deleteById(id);
        return Result.success();
    }

    //timeRange的值有week month month2 month3
    @GetMapping("/lineCharts/{timeRange}")
    public Result lineCharts(@PathVariable String timeRange){
        return Result.success(orderService.getCountByTimeRange(timeRange));
    }
}
